<template>
  <el-tooltip :open-delay="1000"
              placement="top">
    <div slot="content">Managers and Dispatchers can change this to view a specific employee's top priority tickets. <br />This is set to the current user by default.</div>
    <el-select style="width:220px;"
               filterable
               placeholder="User"
               v-model="searchIdentifier"
               @change="changeUser">
      <el-option v-for="(item,index) in identifierUserList"
                 :key="index"
                 :label="item.users"
                 :value="item.id">
      </el-option>
    </el-select>
  </el-tooltip>
</template>

<script>
export default {
  name: 'search-user',
  props: ['nextTicketType', 'searchIdentifier'],
  data () {
    return {
      // searchIdentifier: null,
      identifierUserList: []
    }
  },
  methods: {
    changeUser (val) {
      this.$emit('changeUser', val)
    },
    getIdentifierUserList () {
      let params = {
        status: 1,
        type: this.nextTicketType
      }
      this.$interface.apiManager.getUsersOptionList(params).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.identifierUserList = res.data
        // if (this.$attrs.page === 'nextTicketSetting') {
        // if (this.nextTicketType.replace('dataset','')  === 'autotask' && this.$store.state.bot.autoTaskUserId) {
        //   this.searchIdentifier = this.$store.state.bot.autoTaskUserId
        // } else if (this.nextTicketType.replace('dataset','')  === 'nextTicket' && this.$store.state.bot.nextTicketUserId) {
        //   this.searchIdentifier = this.$store.state.bot.nextTicketUserId
        // } else if (this.nextTicketType.replace('dataset','')  === 'syncro' && this.$store.state.bot.nextTicketUserId) {
        //   this.searchIdentifier = this.$store.state.bot.syncroUserId
        // } else if (this.nextTicketType.replace('dataset','')  === 'kaseyabms' && this.$store.state.bot.nextTicketUserId) {
        //   this.searchIdentifier = this.$store.state.bot.kaseyabmsUserId
        // } else {
        //   let temp
        //   temp = this.identifierUserList.find(e => e.checked)
        //   if (temp) {
        //     this.searchIdentifier = temp.id
        //   }
        // }
        // this.$emit('changeUser', this.searchIdentifier)
        // } else {
        if (!this.$hasPermission('sys:settings:next-ticket-user')) {
          this.identifierUserList = this._.filter(res.data, { userId: this.$store.state.user.id })
        }
        let selfUser = this.identifierUserList.find(e => e.checked)
        if (selfUser) {
          this.searchIdentifier = selfUser.id
          this.$emit('changeUser', this.searchIdentifier)
        }
        // switch (this.nextTicketType) {
        //   case 'autotask':
        //     this.$store.commit('AUTONEXTTICKETUSER', this.searchIdentifier)
        //     break
        //   case 'nextTicket':
        //     this.$store.commit('NEXTTICKETUSER', this.searchIdentifier)
        //     break
        //   case 'syncro':
        //     this.$store.commit('SYNCRONEXTTICKETUSER', this.searchIdentifier)
        //     break
        //   case 'kaseyabms':
        //     this.$store.commit('KASEYABMSNEXTTICKETUSER', this.searchIdentifier)
        //     break
        //   default:
        // }
        // }
      })
    }
  },
  destroyed () {
  },
  created () {
    this.getIdentifierUserList()
  }
}
</script>

<style scoped>

</style>
